package com.example.text.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.text.common.result.Result;
import com.example.text.common.result.ResultFactory;
import com.example.text.entity.AdminRole;
import com.example.text.entity.User;
import com.example.text.service.AdminRoleService;
import com.example.text.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
public class UserController {

    @Autowired
    UserService userService;
    @Autowired
    AdminRoleService adminRoleService;

    @CrossOrigin
    @GetMapping("/api/admin/user/id")
    public Result selectUserId(@RequestParam("username") String username){
        User user= userService.getByUserName(username);
        return ResultFactory.buildSuccessResult(user.getId());
    }

    //个人
    @CrossOrigin
    @GetMapping("/api/admin/user/personal")
    public Result personalUserByUsername(@RequestParam("username") String username){
        System.out.println("进入成功");
        User user= userService.getByUserName(username);
        List<AdminRole> roles;
        roles = adminRoleService.listRolesByUser(user.getUsername());
        user.setRoles(roles);

        return ResultFactory.buildSuccessResult(user);
    }
    @CrossOrigin
    @PutMapping("/api/admin/user/personal")
    public Result editPersonalUser(@RequestBody User requestUser) {
        userService.editUser(requestUser);
        String message = "修改用户信息成功";
        return ResultFactory.buildSuccessResult(message);
    }

    @RequiresPermissions("/api/admin/user")
    @GetMapping("/api/admin/user/{size}/{page}")
    public Result listUsers(@PathVariable("size") int size, @PathVariable("page") int page) throws Exception {
        System.out.println("一页有："+size+"个数据");
        System.out.println("从第"+page+"个页开始");
        Page<User> pages = new Page<>(page,size);
        return ResultFactory.buildSuccessResult(userService.selectUserPage(pages));
    }

    @CrossOrigin
    @PutMapping("/api/admin/user/status")
    public Result updateUserStatus(@RequestBody @Valid User requestUser) {
        System.out.println("传入的值为："+requestUser);
      User user=userService.updateUserEnable(requestUser);
        String message = "用户"+user.getUsername()+"状态更新成功";
        return ResultFactory.buildSuccessResult(message);
    }

    @CrossOrigin
    @PutMapping("/api/admin/user/password")
    public Result resetPassword(@RequestBody @Valid User requestUser) {
        userService.resetPassword(requestUser);
        return ResultFactory.buildSuccessResult("重置密码成功");
    }



    @CrossOrigin
    @PutMapping("/api/admin/user")
    public Result editUser(@RequestBody User requestUser) {
        userService.editUser(requestUser);
        String message = "修改用户信息成功";
        return ResultFactory.buildSuccessResult(message);
    }

    @PostMapping("/api/admin/user/delete")
    public  Result deleteUser(@RequestBody User user) throws Exception {


        if(user.getUsername().equals("admin")){
            String message = "管理者不可以删除";
            System.out.println(message);
            return ResultFactory.buildFailResult(message);
        }

              userService.deleteUser(user);
            return ResultFactory.buildSuccessResult("用户删除成功");
    }

    @PostMapping("/api/admin/user/deletes/{ids}")
    public Result deleteUsers(@PathVariable("ids")String ids){
        String[] userIds = ids.split("\\,");

        //判断批量删除是否存在管理员
        for(String id : userIds) {
             if(id.equals("1")){
                 return ResultFactory.buildFailResult("批量删除中存在管理者不可以删除");
             }
        }
        userService.deleteUsers(userIds);
        return ResultFactory.buildSuccessResult("批量用户删除成功");
    }
}
